package com.orange.quickback.system.controller.user.manage;

import com.orange.quickback.system.api.common.result.QBootResult;
import com.orange.quickback.system.service.user.manage.QBootUserService;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Resource
    QBootUserService qBootUserService;
    @Resource
    ConsumerTokenServices consumerTokenServices;
    @GetMapping("/info")
    public QBootResult getUserInfo(Principal pl){
        return qBootUserService.getUserInfo(pl.getName());
    }

    @PostMapping("/logout")
    public QBootResult revokeToken(HttpServletRequest request) {
        if (consumerTokenServices.revokeToken(request.getHeader("Authorization").substring(6))){
            return QBootResult.buildFail(50008,"注销成功");
        }else{
            return QBootResult.build("注销失败");
        }
    }
}
